<template>
  <view class="weather-page">
    <view class="weather-header">天气预报</view>
    <view class="weather-card" v-if="weatherData">
      <view class="weather-item">明天日期：{{ tomorrowWeather.date }}</view>
      <view class="weather-item">星期：{{ tomorrowWeather.week }}</view>
      <view class="weather-item">最高温：{{ tomorrowWeather.high }}</view>
      <view class="weather-item">最低温：{{ tomorrowWeather.low }}</view>
    </view>
    <view class="loading" v-else>加载中...</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      weatherData: null,
      tomorrowWeather: {}
    }
  },
  onLoad() {
    this.getWeather();
  },
  methods: {
    getWeather() {
      uni.request({
        url: 'http://t.weather.sojson.com/api/weather/city/101230501',
        success: (res) => {
          console.log("success-----" + JSON.stringify(res));
          const forecast = res.data.data.forecast;
          if (forecast && forecast.length > 1) {
            this.tomorrowWeather = {
              date: forecast[1].date,
              week: forecast[1].week,
              high: forecast[1].high,
              low: forecast[1].low
            };
            this.weatherData = true;
          }
        },
        fail: (eMsg) => {
          console.log("request fail-----" + eMsg);
          uni.showToast({
            title: '获取天气失败',
            icon: 'none'
          });
        }
      });
    }
  }
}
</script>

<style scoped>
.weather-page {
  padding: 30rpx;
}
.weather-header {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  text-align: center;
}
.weather-card {
  background-color: #fff;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  padding: 30rpx;
}
.weather-item {
  font-size: 32rpx;
  color: #666;
  margin-bottom: 20rpx;
  line-height: 1.5;
}
.weather-item:last-child {
  margin-bottom: 0;
}
.loading {
  font-size: 32rpx;
  color: #999;
  text-align: center;
  margin-top: 50rpx;
}
</style>